package com.zx.article.reviewRecords.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zx.article.reviewRecords.model.ReviewRecordsModel;
import com.zx.article.reviewRecords.service.IReviewRecordsService;
import com.zx.article.reviewRecords.service.impl.ReviewRecordsServiceImpl;
import com.zx.article.util.FmtRequest;

@WebServlet("/ReviewRecordsServlet")
public class ReviewRecordsServlet extends HttpServlet {

	private static final long serialVersionUID = 4197045252101841314L;
	private FmtRequest fmtRequest = new FmtRequest();
	private IReviewRecordsService recordsService = new ReviewRecordsServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Object result = "未实现方法";
		String action = request.getParameter("action");
		switch (action) {
		case "get":
			result = get(request);
			break;
		case "add":
			result = add(request);
			break;
		}
		fmtRequest.write(response.getWriter(), result);
	}

	private String add(HttpServletRequest request) {
		ReviewRecordsModel reviewRecordsModel = new ReviewRecordsModel();
		reviewRecordsModel.setEditorId(request.getParameter("editor"));
		reviewRecordsModel.setResult(request.getParameter("result"));
		reviewRecordsModel.setMessage(request.getParameter("message"));
		reviewRecordsModel.setDate(request.getParameter("date"));
		reviewRecordsModel.setArticle(Integer.valueOf(request.getParameter("id")));
		return recordsService.add(reviewRecordsModel);
	}

	private Map<String, Object> get(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
//		1-count
		ReviewRecordsModel reviewRecordsModel = new ReviewRecordsModel();
		reviewRecordsModel.setId(Integer.valueOf(request.getParameter("id")));
		int count = recordsService.getCount(reviewRecordsModel);
		map.put("count", count);
//		2-Data
		reviewRecordsModel.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
		reviewRecordsModel.setPageLimit(Integer.parseInt(request.getParameter("pageLimit")));
		reviewRecordsModel.setPageOn(true);
		List<ReviewRecordsModel> list = recordsService.get(reviewRecordsModel);
		map.put("list", list);
		return map;
	}

}
